package servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.LoginBean;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void handLogin(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException{
    	String id = request.getParameter("username");
    	String password = request.getParameter("password");
    	LoginBean loginBean = new LoginBean();
    	String type = loginBean.loginType(id, password);
    	response.setContentType("text/html");
    	
		if(type.equals("employee")){
			HttpSession session=request.getSession();
	    	session.setAttribute("user", id);
			response.getWriter().write("employee");
	    }else if(type.equals("employer")){
	    	HttpSession session=request.getSession();
	    	session.setAttribute("user", id);
	    	response.getWriter().write("employer");
	    }else{
    		response.getWriter().write("usernameOrpassword");
    		//request.getRequestDispatcher("login.jsp").forward(request,response);
	    }
	}

    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handLogin(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handLogin(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
